package hips.tools;

import java.awt.Color;
import java.awt.Graphics;

import hips.Region;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.macro.Interpreter;

@SuppressWarnings("deprecation")
public class RegionRoi extends Roi {
	private static final long serialVersionUID = 1L;
	
	ImagePlus impl;
	Region region;

	public RegionRoi(ImagePlus impl) {
		super(0, 0, impl.getWidth(), impl.getHeight());
		this.impl = impl;
		region = null;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	public Region getRegion() {
		return region;
	}

	public void draw(Graphics g) {
		if (region == null || ic == null) {
			return;
		}
		Color color = ROIColor;
		if (strokeColor != null) {
			color = strokeColor;
		}
		if (fillColor != null) {
			color = fillColor;
		}
		if (Interpreter.isBatchMode() && ic.getDisplayList() != null
				&& strokeColor == null && fillColor == null) {
			return;
		}
		g.setColor(color);
		for (int i = 0; i < region.getSize(); i++) {
			int pixel = region.getPixelPosition(i);
			int px = pixel % impl.getWidth();
			int py = pixel / impl.getWidth();
			int x1 = ic.screenX(px);
			int y1 = ic.screenY(py);
			int x2 = ic.screenX(px + 1);
			int y2 = ic.screenY(py + 1);
			g.fillRect(x1, y1, x2 - x1, y2 - y1);
		}
		if (updateFullWindow) {
			updateFullWindow = false;
			imp.draw();
		}
	}
}
